string variable
String variables are sequences of UBYTE characters. Strings generally contain ASCII
text, but can hold arbitrary byte sequences. Strings are automatically elastic,
meaning they automatically resize to contain whatever number of bytes are put into them.
When a string resizes, its location in memory may change, as when a longer string
is assigned and there is insufficient room after the string to store the extra bytes.
For this reason, every string has a handle, an XLONG value at a fixed location in
memory (or CPU register) that always contains the current address of the string data.
If the string is empty, the handle contains zero.
Empty strings are FALSE, while strings with any contents are TRUE. This makes
testing for empty strings simple and efficient, as illustrated by the following examples:
IF a$ THEN PRINT "a$ has contents"
IFZ a$ THEN PRINT "a$ is empty"
brace notation extract
Brace notation can be used to extract individual bytes from strings. thisByte =
var${byteOffset} extracts the UBYTE from var$ that is byteOffset characters from the
beginning of var$, and assigns it to numeric variable thisByte. The first character
is at byteOffset = 0.
Brace notation extract is hundreds of times faster than ASC(MID$(var$,n,1)), which is the
conventional BASIC way to extract one byte from a string. It is faster because
intrinsic function call overhead is eliminated, string space management is avoided, and
erroneous access outside the string is not restricted (unless bounds checking is enabled).
The two argument ASC() intrinsic (as in ASC(var$,pos) ) as a safe, intermediate efficiency
alternative to brace notation extract. Brace notation extract is an advanced feature
to be employed with care.
brace notation assign
Brace notation can also be used to assign individual bytes into strings. var${byteOffset}
= newByte replaces the existing character at byteOffset in var$ with the low byte of
newByte.
For the same reasons as brace notation extract, brace notation assign is much faster than
conventional alternatives, and is an advanced feature to be used with care.
Brace notation assign has an additional danger. Unlike brace notation extract, it
writes to memory. Since bounds checking is not automatically performed, brace
notation assign can write outside the memory allocated for a string, modifying other
variables and memory allocation headers, either of which can crash the program and
possibly even the development environment. It is particularly important, therefore,
to avoid invalid offset values in brace notation assign.